package org.apitool.main;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TestBase {

	protected static HttpClient client = new HttpClient();

	private IProccess process;

	public TestBase() {

		// client.login(HttpClient.LOGIN_TYPE_CLIENT, "appadmin", "appadmin111",
		// "M81B08BJCBP030186");
	}

	public TestBase(String consumerkey, String consumerSecret, String username, String pwd, String sn) {
		client = new HttpClient(consumerkey, consumerSecret);
		client.login(HttpClient.LOGIN_TYPE_CLIENT, username, pwd, sn);
	}

	public void login() {
		client.login(HttpClient.LOGIN_TYPE_CLIENT, "appadmin", "appadmin111", "M81B08BJCBP030186");
	}

	public boolean login(String username, String password, String sn) {
		return client.login(HttpClient.LOGIN_TYPE_CLIENT, username, password, sn);
	}

	public boolean login(String loginType, String username, String password, String sn) {
		if (loginType.equalsIgnoreCase("sn")) {
			return client.login(HttpClient.LOGIN_TYPE_SN, username, password, sn);
		} else {
			return client.login(HttpClient.LOGIN_TYPE_CLIENT, username, password, sn);
		}

	}

	public String post(String url, List<PostParameter> postParams) {
		String str = null;
		try {
			str = client.doHttpRequest(HttpClient.METHOD_POST, url, postParams);

			return str;
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	public String post(String url, Map param) {
		String str = null;
		try {
			List<PostParameter> postParams = new ArrayList<PostParameter>();
			if (param != null) {
				Iterator it = param.keySet().iterator();
				while (it.hasNext()) {
					String key = it.next().toString();
					PostParameter tp = new PostParameter(key, param.get(key).toString());
					postParams.add(tp);
				}
			}
			str = client.doHttpRequest(HttpClient.METHOD_POST, url, postParams);

			return str;
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	public String get(String url) {
		String str = null;
		try {
			str = client.doHttpRequest(HttpClient.METHOD_GET, url, null);

			return str;
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	public IProccess getProcess() {
		return process;
	}

	public void setProcess(IProccess process) {
		this.process = process;
	}

	public HttpClient getClient() {
		return this.client;
	}

}
